/*
    Copyright 2019 Zheyong Fan
    This file is part of GPUGA.
    GPUGA is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    GPUGA is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with GPUGA.  If not, see <http://www.gnu.org/licenses/>.
*/

/*----------------------------------------------------------------------------80
Some functions for dealing with text files. Written by Mikko Ervasti.
------------------------------------------------------------------------------*/

#include "error.cuh"
#include "read_file.cuh"
#include <ctype.h>
#include <errno.h>

// Read the input file to memory
char* get_file_contents(char* filename)
{
  char* contents;
  int contents_size;
  FILE* in = my_fopen(filename, "r");
  // Find file size
  fseek(in, 0, SEEK_END);
  contents_size = ftell(in);
  rewind(in);
  MY_MALLOC(contents, char, contents_size + 1);
  int size_read_in = fread(contents, sizeof(char), contents_size, in);
  if (size_read_in != contents_size) {
    print_error("File size mismatch.");
  }
  fclose(in);
  contents[contents_size] = '\0'; // Assures proper null termination
  return contents;
}

// Parse a single row
char* row_find_param(char* s, char* param[], int* num_param)
{
  *num_param = 0;
  int start_new_word = 1, comment_found = 0;
  if (s == NULL)
    return NULL;
  while (*s) {
    if (*s == '\n') {
      *s = '\0';
      return s + sizeof(char);
    } else if (comment_found) {
    } // Do nothing
    else if (*s == '#') {
      *s = '\0';
      comment_found = 1;
    } else if (isspace(*s)) {
      *s = '\0';
      start_new_word = 1;
    } else if (start_new_word) {
      param[*num_param] = s;
      ++(*num_param);
      start_new_word = 0;
    }
    ++s;
  }
  return NULL;
}

int is_valid_int(const char* s, int* result)
{
  if (s == NULL || *s == '\0') {
    return 0;
  }
  char* p;
  errno = 0;
  *result = (int)strtol(s, &p, 0);
  if (errno != 0 || s == p || *p != 0) {
    return 0;
  } else {
    return 1;
  }
}

int is_valid_real(const char* s, float* result)
{
  if (s == NULL || *s == '\0') {
    return 0;
  }
  char* p;
  errno = 0;
  *result = strtod(s, &p);
  if (errno != 0 || s == p || *p != 0) {
    return 0;
  } else {
    return 1;
  }
}
